const userModel = require("../db/mysql.js");
const md5 = require("md5");
const createToken = require("../token/createToken.js");

exports.postSignin = async ctx => {
  let { name, pass } = ctx.request.body;
  await userModel
    .findDataByName(name)
    .then(async result => {
      let res = result;
      if (
        res.length &&
        name === res[0]["name"] &&
        md5(pass) === res[0]["pass"]
      ) {
        let token = createToken(res[0]);
        ctx.body = {
          code: 200,
          message: "登录成功！",
          data: {
            name,
            token
          },
         
        };
      } else {
        ctx.body = {
          code: 500,
          message: "用户名或密码错误"
        };
        console.log("用户名或密码错误!");
      }
    })
    .catch(err => {
      ctx.body = {
        code: 500,
        msg: err,
        data: {}
      };
    });
};
